Option Strict Off
Option Explicit On
Imports Microsoft.VisualBasic.PowerPacks
Namespace BMLERP
	Friend Class frmACR500
		Inherits System.Windows.Forms.Form
		Dim NewTrans As Byte



		Private Sub cboARCCD_ClickEvent(ByVal eventSender As System.Object, ByVal eventArgs As AxMSDataListLib.DDataComboEvents_ClickEvent) Handles cboARCCD.ClickEvent
			Dim RecCCI As ADODB.Recordset
			If eventArgs.area = 2 Then
				txtARCCD.Text = cboARCCD.CtlText
				cboARCCD.Visible = False
				RecCCI = DBconn.Execute("Select * from CCI where CICCD='" & txtARCCD.Text & "' order by CIDTE DESC")
				If Not RecCCI.EOF Then
					txtARCER.Text = RecCCI.Fields("CICCR").Value
				Else
					txtARCER.Text = CStr(0)
				End If
			End If
		End Sub

		Private Sub cboARCNO_ClickEvent(ByVal eventSender As System.Object, ByVal eventArgs As AxMSDataListLib.DDataComboEvents_ClickEvent) Handles cboARCNO.ClickEvent
			If eventArgs.area = 2 Then
				txtARCNO.Text = cboARCNO.BoundText
				txtCMNME.Text = cboARCNO.CtlText
				cboARCNO.Visible = False
				txtARCCD.Focus()
				Adodc2.Refresh()
			End If
		End Sub

		Private Sub cmdAddnew_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdAddNew.Click
			NewTrans = True
			ISEDITING = True
			ISADDING = True
			WriteToScreen()
			ButtonStatus()
		End Sub

		Private Sub cmdCancel_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdCancel.Click
			NewTrans = False
			ISADDING = False
			ISEDITING = False
			ButtonStatus()
		End Sub

		Private Sub cmdEdit_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdEdit.Click
			ISEDITING = True
			'ISADDING = True
			WriteToScreen()
			ButtonStatus()

		End Sub

		Private Sub cmdexit_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdExit.Click
			Me.Close()
		End Sub

		Private Sub cmdNewItem_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdNewItem.Click
			ISEDITING = True
			ISADDING = True
			WriteToScreen()
			ButtonStatus()
		End Sub

		Private Sub cmdReview_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdReview.Click
			Dim myform As New frmReview
			myform.Tag = "ACR500"
			myform.Show()
			'frmReview.Tag = "ACR500"
			'frmReview/.ShowDialog()
			ISEDITING = False
			ISADDING = False
			'If frmReview.Tag <> "" Then
			'txtARDOC.Text = frmReview.GridList.Columns(1).Text
			'txtARRFD.Text = frmReview.GridList.Columns(2).Text
			If myform.Tag <> "" Then
				myform.Text = myform.GridList.Columns(1).Text
				myform.Text = myform.GridList.Columns(2).Text

				myform.Close()
				'frmReview.Close()
				'txtBLLNE = 1
				WriteToScreen()
			End If
		End Sub

		Private Sub cmdSave_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdSave.Click
			WriteToFile()
			ISADDING = False
			ISEDITING = False
			NewTrans = False
			ButtonStatus()
		End Sub

		Private Sub frmACR500_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
			OpenFile()
			VB6_AddADODataBinding()
			cboARCNO.RowSource = ADOBind_Adodc1.DataSource
			cboARRFD.RowSource = ADOBind_Adodc2.DataSource
			Dim myDataCaptions As String() = {"Ref Doc", "Doc", "Total Amt", "Payment", "Balance", "Base Amt", "Base Payment", "Base Balance"}
			Dim myDataFields As String() = {"ARRFD", "ARDOC", "BHAMT", "ARAMT", "ARBAL", "BHBAM", "ARBAM", "ARBBL"}
			setDataGridView(myDataCaptions, myDataFields, Me.grdACR)

		End Sub



		Private Sub grdACR_ClickEvent(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles grdACR.ClickEvent
			Dim grdBBL As Object
			txtARRFD.Text = grdBBL.Columns(1).value
			txtARDOC.Text = grdBBL.Columns(0).value
			WriteToScreen()
			Adodc4.Recordset.AbsolutePosition = SELECTROW
			ISADDING = False
			ISEDITING = False
			ButtonStatus()
		End Sub

		Private Sub grdACR_RowColChange(ByVal eventSender As System.Object, ByVal eventArgs As AxMSDataGridLib.DDataGridEvents_RowColChangeEvent) Handles grdACR.RowColChange
			SELECTROW = Adodc4.Recordset.Bookmark
		End Sub

		Private Sub txtARBAM_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtARBAM.KeyPress
			Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
			If KeyAscii = 13 Then
				If KeyAscii = 13 Then
					txtARAMT.Text = CStr(Val(txtARBAM.Text) * Val(txtARCER.Text))
					txtARAMT.Focus()
					CalculateACR()
				End If
			End If
			eventArgs.KeyChar = Chr(KeyAscii)
			If KeyAscii = 0 Then
				eventArgs.Handled = True
			End If
		End Sub

		Private Sub txtARCCD_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtARCCD.KeyPress
			Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
			Dim Result As String
			Dim RecCCI As ADODB.Recordset
			If KeyAscii = 13 Then
				Result = Find("CCM", "CMCCD", "WHERE CMCCD='" & txtARCCD.Text & "'")
				If Result <> "" Then
					RecCCI = DBconn.Execute("Select * from CCI where CICCD='" & txtARCCD.Text & "' order by CIDTE DESC")
					If Not RecCCI.EOF Then
						txtARCER.Text = RecCCI.Fields("CICCR").Value
					Else
						txtARCER.Text = CStr(0)
					End If
					txtARCER.Focus()
				Else
					cboARCCD.Visible = True
					cboARCCD.Focus()
				End If
			End If

			eventArgs.KeyChar = Chr(KeyAscii)
			If KeyAscii = 0 Then
				eventArgs.Handled = True
			End If
		End Sub

		Private Sub txtARCNO_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtARCNO.KeyPress
			Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
			Dim Result As String
			If KeyAscii = 13 Then
				Result = Find("RCM", "CMNME", "WHERE CMCNO='" & txtARCNO.Text & "'")
				If Result = "" Then
					cboARCNO.Visible = True
					cboARCNO.Focus()
				Else
					cboARCNO.Visible = False
					txtCMNME.Text = Result
					txtARCCD.Focus()
					Adodc2.Refresh()
				End If
			End If
			eventArgs.KeyChar = Chr(KeyAscii)
			If KeyAscii = 0 Then
				eventArgs.Handled = True
			End If
		End Sub
		Sub OpenFile()
			Dim sql As Object
			Adodc1.ConnectionString = ConnectString
			Adodc1.ConnectionTimeout = 10
			Adodc1.CommandType = ADODB.CommandTypeEnum.adCmdText
			Adodc1.RecordSource = "Select * from RCM"
			Adodc1.Refresh()

			Adodc2.ConnectionString = ConnectString
			Adodc2.ConnectionTimeout = 10
			Adodc2.CommandType = ADODB.CommandTypeEnum.adCmdText
			Adodc2.RecordSource = "SELECT ACR.* From ACR WHERE (ARDOC = ARRFD) AND (ARCNO='" & txtARCNO.Text & "')"
			Adodc2.Refresh()

			Adodc3.ConnectionString = ConnectString
			Adodc3.ConnectionTimeout = 10
			Adodc3.CommandType = ADODB.CommandTypeEnum.adCmdText
			sql = "Select * from CCM"
			Adodc3.RecordSource = sql
			Adodc3.Refresh()

			Adodc4.ConnectionString = ConnectString
			Adodc4.ConnectionTimeout = 10
			Adodc4.CommandType = ADODB.CommandTypeEnum.adCmdText
			Adodc4.RecordSource = "SELECT ACR.* From ACR WHERE (ARDOC <> ARRFD)" & " and (ARDOC='" & txtARDOC.Text & "') ORDER BY ARRFD"
			Adodc4.Refresh()

		End Sub
		Sub WriteToFile()
			Dim sql As Object
			If IsDate(txtARDTE.Text) Then txtARDTE.Text = VB6.Format(txtARDTE.Text, "yyyy-mm-dd hh:mm:ss")

			If ISADDING Then
				txtARDOC.Text = TakeOrderNumberACN("CNNPN", True)
				sql = "Insert Into ACR (ARDOC,ARDTE,ARCNO,ARCCD,ARCER" & ",ARRFD,ARBAM,ARAMT,ARBBL,ARBAL) values " & "('" & txtARDOC.Text & "','" & txtARDTE.Text & "','" & txtARCNO.Text & "','" & txtARCCD.Text & "'," & txtARCER.Text & ",'" & txtARRFD.Text & "'," & txtARBAM.Text & "," & txtARAMT.Text & "," & txtARBBL.Text & "," & txtARBAL.Text & ")"
			Else
				sql = "UPDATE ACR SET ARDOC='" & txtARDOC.Text & "', ARRFD='" & txtARRFD.Text & "',ARDTE='" & txtARDTE.Text & "',ARCNO='" & txtARCNO.Text & "',ARCCD='" & txtARCCD.Text & "',ARCER=" & txtARCER.Text & ",ARRFD='" & txtARRFD.Text & "',ARBAM=" & txtARBAM.Text & ",ARAMT=" & txtARAMT.Text & ",ARBBL=" & txtARBBL.Text & ",ARBAL=" & txtARBAL.Text & " where  ARDOC='" & txtARDOC.Text & "' and ARRFD='" & txtARRFD.Text & "'"
			End If
			DBconn.Execute(sql)

			Adodc4.RecordSource = "Select ACR.* from ACR " & "  WHERE ARRFD='" & txtARRFD.Text & "'"
			Adodc4.Refresh()
		End Sub
		Sub WriteToScreen()
			If NewTrans Then
				txtARDOC.Text = ""
				txtARDTE.Text = CStr(WorkingDate)
				txtARCNO.Text = ""
				txtCMNME.Text = ""
				txtARCCD.Text = ""
				txtARCER.Text = ""
				cboARCCD.defText = ""
			End If
			cboARRFD.Visible = False
			Dim RecACR As ADODB.Recordset
			If ISADDING Then
				'    txtARRFD = ""
				'    cboARRFD = ""
				txtARBAM.Text = CStr(0)
				txtARAMT.Text = CStr(0)
				txtARBBL.Text = CStr(0)
				txtARBAL.Text = CStr(0)
			Else
				RecACR = DBconn.Execute("Select * from ACR where ARDOC='" & txtARDOC.Text & "' AND ARRFD='" & txtARRFD.Text & "'")
				With RecACR
					txtARDOC.Text = .Fields("ARDOC").Value
					txtARDTE.Text = .Fields("ARDTE").Value
					txtARCNO.Text = .Fields("ARCNO").Value
					txtCMNME.Text = Find("RCM", "CMNME", " WHERE CMCNO='" & txtARCNO.Text & "'")
					txtARCCD.Text = .Fields("ARCCD").Value
					txtARCER.Text = .Fields("ARCER").Value
					cboARCCD.defText = .Fields("ARCCD").Value
					txtARRFD.Text = .Fields("ARRFD").Value

					txtARBAM.Text = .Fields("ARBAM").Value
					txtARAMT.Text = .Fields("ARAMT").Value
					txtARBBL.Text = .Fields("ARBBL").Value
					txtARBAL.Text = .Fields("ARBAL").Value
				End With
			End If

			Adodc4.RecordSource = "Select ACR.* from ACR " & "  WHERE  ARRFD='" & txtARRFD.Text & "'"
			Adodc4.Refresh()
		End Sub

		Sub ButtonStatus()
			cmdAddNew.Enabled = Not ISEDITING
			cmdReview.Enabled = Not ISEDITING
			cmdNewItem.Enabled = Not ISEDITING
			cmdEdit.Enabled = Not ISEDITING
			cmdSave.Enabled = ISEDITING
			cmdCancel.Enabled = ISEDITING
			FrameARC.Enabled = NewTrans
			FrameLine.Enabled = ISEDITING
			FrameACRLine.Enabled = ISEDITING
		End Sub

		Private Sub txtARRFD_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtARRFD.KeyPress
			Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
			Dim Result As Object
			If KeyAscii = 13 Then
				Result = Find("ACR", "ARRFD", " WHERE (ARDOC = ARRFD) AND (ARCNO='" & txtARCNO.Text & "')")
				If Result = "" Then
					MsgBox("Khong co so Ref No nay")
					cboARRFD.Visible = True
					cboARRFD.Focus()
				Else
					txtARBAM.Focus()
				End If
			End If
			eventArgs.KeyChar = Chr(KeyAscii)
			If KeyAscii = 0 Then
				eventArgs.Handled = True
			End If
		End Sub
		Sub CalculateACR()
			Dim Payment, recACRTotal, PaymentBase As Object
			Dim recACRPayment As ADODB.Recordset
			recACRTotal = DBconn.Execute("Select * from ACR where ARDOC='" & txtARDOC.Text & "'")

			If Not recACRTotal.EOF Then
				txtARBAM1.Text = recACRTotal!ARBAM
				txtARAMT1.Text = recACRTotal!ARAMT
			End If
			recACRPayment = DBconn.Execute("Select * from ACR where ARRFD='" & txtARDOC.Text & "' AND ARRFD<>ARDOC AND ARDOC<" & txtARDOC.Text)
			Payment = 0
			PaymentBase = 0
			With recACRPayment
				Do While Not .EOF
					Payment = Payment + .Fields("ARAMT").Value
					PaymentBase = PaymentBase + .Fields("ARBAM").Value
					.MoveNext()
				Loop
				.Close()
			End With

			txtARBBL.Text = Val(txtARBAM1.Text) - PaymentBase - Val(txtARBAM.Text)
			txtARBAL.Text = Val(txtARAMT1.Text) - Payment - Val(txtARAMT.Text)
		End Sub
	End Class
End Namespace